package com.ruoyi.report_hntjg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcjlbBaDetail;
import com.ruoyi.report_hntjg.domain.bo.ReportHntjgHntqdhtfbgBsInfoBo;
import com.ruoyi.report_hntjg.mapper.ReportHntjgHntqdhtfbgBaDetailMapper;
import com.ruoyi.report_hntjg.mapper.ReportHntjgHntqdhtfbgBsInfoMapper;
import com.ruoyi.report_hntjg.service.IReportHntjgHntqdhtfbgBsInfoService;
import com.ruoyi.report_hntjg.domain.ReportHntjgHntqdhtfbgBaDetail;
import com.ruoyi.report_hntjg.domain.ReportHntjgHntqdhtfbgBsInfo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgHntqdhtfbgBaDetailVo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgHntqdhtfbgBsInfoVo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 混凝土强度试验检测报告（回弹法）Service业务层处理
 *
 * @author tll
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class ReportHntjgHntqdhtfbgBsInfoServiceImpl implements IReportHntjgHntqdhtfbgBsInfoService {

    private final ReportHntjgHntqdhtfbgBsInfoMapper baseMapper;
    private final ReportHntjgHntqdhtfbgBaDetailMapper reportHntjgHntqdhtfbgBaDetailMapper;

    /**
     * 查询混凝土强度试验检测报告（回弹法）
     */
    @Override
    @FIllBasicInfo
    public ReportHntjgHntqdhtfbgBsInfoVo queryById(Long excelId){
        // 主表
        ReportHntjgHntqdhtfbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportHntjgHntqdhtfbgBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportHntjgHntqdhtfbgBaDetail::getCreateTime);
        lqw.orderByAsc(ReportHntjgHntqdhtfbgBaDetail::getSort);
        List<ReportHntjgHntqdhtfbgBaDetailVo> detailList = reportHntjgHntqdhtfbgBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询混凝土强度试验检测报告（回弹法）列表
     */
    @Override
    public TableDataInfo<ReportHntjgHntqdhtfbgBsInfoVo> queryPageList(ReportHntjgHntqdhtfbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportHntjgHntqdhtfbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土强度试验检测报告（回弹法）列表
     */
    @Override
    public List<ReportHntjgHntqdhtfbgBsInfoVo> queryList(ReportHntjgHntqdhtfbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportHntjgHntqdhtfbgBsInfo> buildQueryWrapper(ReportHntjgHntqdhtfbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportHntjgHntqdhtfbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportHntjgHntqdhtfbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportHntjgHntqdhtfbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportHntjgHntqdhtfbgBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportHntjgHntqdhtfbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportHntjgHntqdhtfbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportHntjgHntqdhtfbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportHntjgHntqdhtfbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportHntjgHntqdhtfbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportHntjgHntqdhtfbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportHntjgHntqdhtfbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportHntjgHntqdhtfbgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportHntjgHntqdhtfbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportHntjgHntqdhtfbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportHntjgHntqdhtfbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportHntjgHntqdhtfbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportHntjgHntqdhtfbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportHntjgHntqdhtfbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportHntjgHntqdhtfbgBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportHntjgHntqdhtfbgBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportHntjgHntqdhtfbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportHntjgHntqdhtfbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportHntjgHntqdhtfbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportHntjgHntqdhtfbgBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportHntjgHntqdhtfbgBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportHntjgHntqdhtfbgBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportHntjgHntqdhtfbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportHntjgHntqdhtfbgBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(bo.getJincrq() != null, ReportHntjgHntqdhtfbgBsInfo::getJincrq, bo.getJincrq());
        lqw.eq(bo.getDaibsl() != null, ReportHntjgHntqdhtfbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getChand()), ReportHntjgHntqdhtfbgBsInfo::getChand, bo.getChand());
        lqw.eq(bo.getRefExcelId() != null, ReportHntjgHntqdhtfbgBsInfo::getRefExcelId, bo.getRefExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportHntjgHntqdhtfbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportHntjgHntqdhtfbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportHntjgHntqdhtfbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportHntjgHntqdhtfbgBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增混凝土强度试验检测报告（回弹法）
     */
    @Override
    public Boolean insertByBo(ReportHntjgHntqdhtfbgBsInfoBo bo) {
        ReportHntjgHntqdhtfbgBsInfo add = BeanUtil.toBean(bo, ReportHntjgHntqdhtfbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改混凝土强度试验检测报告（回弹法）
     */
    @Override
    public Boolean updateByBo(ReportHntjgHntqdhtfbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        // 主表
        ReportHntjgHntqdhtfbgBsInfo update = BeanUtil.toBean(bo, ReportHntjgHntqdhtfbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            reportHntjgHntqdhtfbgBaDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportHntjgHntqdhtfbgBaDetail.class));
        }
        return flag;

//        boolean upd = baseMapper.updateById(update) > 0;
//        // 子表
//        boolean upd2 = false;
//        boolean insert = false;
//        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportHntjgHntqdhtfbgBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportHntjgHntqdhtfbgBaDetail> list = reportHntjgHntqdhtfbgBaDetailMapper.selectList(queryWrapper);
//            for (ReportHntjgHntqdhtfbgBaDetail det : bo.getDetailList()) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = reportHntjgHntqdhtfbgBaDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = reportHntjgHntqdhtfbgBaDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    private void fillExcelResult(ReportHntjgHntqdhtfbgBsInfoBo bo) {
//        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
//            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
//                if ("不符合".equals(detailVo.getJiegpd())) {
//                    bo.setExcelResult(2);
//                    return;
//                }
//            }
//            bo.setExcelResult(1);
//        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportHntjgHntqdhtfbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土强度试验检测报告（回弹法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成回弹法检测结论
     *
     * @param vo
     * @return
     */
    @Override
    public ReportHntjgHntqdhtfbgBsInfoVo jcjl(ReportHntjgHntqdhtfbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，");
        result.append("样品名称xxx");
        result.append("的抗压强度符合设计技术要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }


}
